// 提示消息工具函数

/**
 * 显示提示消息
 * @param {string} message - 提示消息内容
 * @param {string} type - 提示类型：success, error, warning, info
 * @param {number} duration - 显示时长(毫秒)，默认3000
 */
export const showToast = (message, type = 'info', duration = 3000) => {
  // 检查是否已存在toast容器，如果没有则创建
  let toastContainer = document.querySelector('.toast-container');
  if (!toastContainer) {
    toastContainer = document.createElement('div');
    toastContainer.className = 'toast-container fixed top-4 right-4 z-50 flex flex-col gap-2';
    document.body.appendChild(toastContainer);
  }

  // 创建新的toast元素
  const toast = document.createElement('div');
  toast.className = `px-4 py-3 rounded-md shadow-lg transition-all duration-300 transform translate-x-full opacity-0`;
  toast.textContent = message;

  // 根据类型设置不同的样式
  switch (type) {
    case 'success':
      toast.classList.add('bg-green-100', 'text-green-800', 'border-l-4', 'border-green-500');
      break;
    case 'error':
      toast.classList.add('bg-red-100', 'text-red-800', 'border-l-4', 'border-red-500');
      break;
    case 'warning':
      toast.classList.add('bg-yellow-100', 'text-yellow-800', 'border-l-4', 'border-yellow-500');
      break;
    case 'info':
    default:
      toast.classList.add('bg-blue-100', 'text-blue-800', 'border-l-4', 'border-blue-500');
      break;
  }

  // 添加到容器
  toastContainer.appendChild(toast);

  // 显示动画
  setTimeout(() => {
    toast.classList.remove('translate-x-full', 'opacity-0');
    toast.classList.add('translate-x-0', 'opacity-100');
  }, 10);

  // 设置自动关闭
  setTimeout(() => {
    toast.classList.remove('translate-x-0', 'opacity-100');
    toast.classList.add('translate-x-full', 'opacity-0');
    
    // 动画结束后移除元素
    setTimeout(() => {
      if (toastContainer.contains(toast)) {
        toastContainer.removeChild(toast);
      }
      
      // 如果容器为空，移除容器
      if (toastContainer.children.length === 0) {
        document.body.removeChild(toastContainer);
      }
    }, 300);
  }, duration);

  // 返回toast元素，方便外部操作
  return toast;
};

/**
 * 显示成功提示
 * @param {string} message - 提示消息
 * @param {number} duration - 显示时长
 */
export const showSuccessToast = (message, duration = 3000) => {
  return showToast(message, 'success', duration);
};

/**
 * 显示错误提示
 * @param {string} message - 提示消息
 * @param {number} duration - 显示时长
 */
export const showErrorToast = (message, duration = 3000) => {
  return showToast(message, 'error', duration);
};

/**
 * 显示警告提示
 * @param {string} message - 提示消息
 * @param {number} duration - 显示时长
 */
export const showWarningToast = (message, duration = 3000) => {
  return showToast(message, 'warning', duration);
};

/**
 * 显示信息提示
 * @param {string} message - 提示消息
 * @param {number} duration - 显示时长
 */
export const showInfoToast = (message, duration = 3000) => {
  return showToast(message, 'info', duration);
};

/**
 * 关闭所有提示
 */
export const closeAllToasts = () => {
  const toastContainer = document.querySelector('.toast-container');
  if (toastContainer) {
    const toasts = toastContainer.querySelectorAll('.toast-container > div');
    toasts.forEach(toast => {
      toast.classList.remove('translate-x-0', 'opacity-100');
      toast.classList.add('translate-x-full', 'opacity-0');
    });
    
    // 动画结束后移除容器
    setTimeout(() => {
      if (document.body.contains(toastContainer)) {
        document.body.removeChild(toastContainer);
      }
    }, 300);
  }
};